package com.moqianfan.mcuassistant.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.bean.User;
import com.moqianfan.mcuassistant.utils.GsonUtils;
import com.moqianfan.mcuassistant.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2017/6/14.
 * 注册
 */

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_user_pass1)
    EditText editPass1;
    @BindView(R.id.edit_user_pass2)
    EditText editPass2;
    @BindView(R.id.edit_email)
    EditText editEmail;

    private ProgressDialog dialog;

    private static String TAG = "RegisterActivity";

    @Override
    public void findViews() {
        setContentView(R.layout.activity_regiter);
    }

    @Override
    public void initViews() {
        dialog = new ProgressDialog(this);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setMessage("正在注册，请稍后...");
    }

    @Override
    public void initDatas() {

    }

    @OnClick({R.id.btn_register, R.id.image_close})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    public void register() {
        if (TextUtils.isEmpty(editName.getText().toString())) {
            showToastShort("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(editEmail.getText().toString())) {
            showToastShort("邮件地址不能为空");
            return;
        }
        if (TextUtils.isEmpty(editPass1.getText().toString())) {
            showToastShort("密码不能为空");
            return;
        }
        if (!editPass1.getText().toString().equals(editPass2.getText().toString())) {
            showToastShort("两次密码输入不一致");
            return;
        }

        if (null != dialog && !dialog.isShowing())
            dialog.show();

        User user = new User();
        user.setUsername(editName.getText().toString());
        user.setPassword(editPass1.getText().toString());
        user.setEmail(editEmail.getText().toString());
        user.signUp(new SaveListener<User>() {
            @Override
            public void done(User bmobUser, BmobException e) {
                if (null == e) {
                    sendMsg(GsonUtils.GsonString(bmobUser), 0x20, handler);
                } else {
                    LogUtil.d(TAG, e.toString());
                    switch (e.getErrorCode()) {
                        case 202:
                            sendMsg("用户名已存在", 0x21, handler);
                            break;
                        case 203:
                            sendMsg("该邮箱已被注册", 0x21, handler);
                            break;
                    }
                }
            }
        });
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            switch (msg.what) {
                case 0x20:
                    EventBus.getDefault().post(GsonUtils.GsonToBean(bundle.getString("msg"), User.class));
                    showToastShort("注册成功");
                    finish();
                    break;
                case 0x21:
                    showToastShort(bundle.getString("msg"));
                    break;
            }
            if (null != dialog && dialog.isShowing())
                dialog.dismiss();
            return true;
        }
    });
}
